#pragma once

#include <iostream>
#include <unordered_map>
#include <fstream>

#include "log.hpp"
#include "inetaddr.hpp"

namespace Dictionary
{
#define DIC_PATHNAME "./dictionary"
    // #define DIC_DELIMITER ": "
    static std::string dic_delimiter(": ");
    class Dic
    {
    public:
        Dic(std::string pathname = DIC_PATHNAME)
            : _pathname(pathname)
        {
        }
        void LoadDic()
        {
            // 1.打开文件流
            std::ifstream dicin(_pathname);
            if (!dicin.is_open())
            {
                Local_Log::LOG(Local_Log::LogLever::LFATAL) << "ifstream error";
            }
            // 2.循环读一行文件
            std::string dic_line;
            // getline用while循环判断的原理是：流对象被隐式转换成了bool值，即重载了operator bool()
            while (std::getline(dicin, dic_line))
            {
                // 3.查找分割符
                size_t del_index = dic_line.find(dic_delimiter);
                // 4.处理异常字段
                if (del_index >= dic_line.size())
                {
                    Local_Log::LOG(Local_Log::LogLever::LINFO) << "del_index error";
                    continue;
                }
                // 5.把左右两边分开
                std::string english(dic_line, 0, del_index);
                std::string chilese(dic_line, del_index + 2, dic_line.size());
                // 6.处理异常字段
                if (english.empty())
                {
                    Local_Log::LOG(Local_Log::LogLever::LINFO) << "english error";
                    continue;
                }
                else if (chilese.empty())
                {
                    Local_Log::LOG(Local_Log::LogLever::LINFO) << "chilese error";
                    continue;
                }
                // 7.插入到哈希表
                _dictionary[english] = chilese;
            }
        }
        void print()
        {
            for (auto it : _dictionary)
            {
                std::cout << it.first << " == " << it.second << std::endl;
            }
        }
        std::string Translation(const std::string &english, Local_Inetaddr::Inetaddr &addr)
        {
            // 1.查找
            auto it = _dictionary.find(english);
            if (it == _dictionary.end())
            {
                // 2.失败
                std::cout << "[ip= " << addr.GetIp() << "\t port= " << addr.GetPort() << "] #" << english << "\t is None" << std::endl;
                return "Nonr";
            }
            // 3.成功
            std::string chinese = it->second;
            std::cout << "[ip= " << addr.GetIp() << "\t port= " << addr.GetPort() << "] #" << english << "\t is " << chinese << std::endl;
            return chinese;
        }
        std::string Translation(const std::string &english)
        {
            // 1.查找
            auto it = _dictionary.find(english);
            if (it == _dictionary.end())
            {
                // 2.失败
                std::cout << "[ip= port= ] #" << english << "\t is None" << std::endl;
                return "Nonr";
            }
            // 3.成功
            std::string chinese = it->second;
            std::cout << "[ip= port= ] #" << english << "\t is " << chinese << std::endl;
            return chinese;
        }
        ~Dic()
        {
        }

    private:
        std::string _pathname;
        std::unordered_map<std::string, std::string> _dictionary;
    };

}